package top.huxy.boot.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.huxy.boot.mapper.TagMapper;
import top.huxy.boot.model.vo.VOTag;
import top.huxy.boot.service.TagService;

import java.util.HashSet;
import java.util.List;

/**
 * @Classname TagServiceImpl
 * @Description TagServiceImpl
 * @Date 2021/9/1 9:49
 * @Created by huxy
 */
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    TagMapper tagMapper;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<VOTag> getAllTags() {
        return tagMapper.getAllTags();
    }

    @Override
    public boolean addStringTag(String tag) {
        return 1 == tagMapper.addStringTag(tag, System.currentTimeMillis());
    }

    @Override
    public int queryTagId(String tag) {
        return tagMapper.queryTagId(tag);
    }

    @Override
    public boolean applyTag(int blogId, Integer tagId) {
        return 1 == tagMapper.applyTag(blogId, tagId);
    }

    @Override
    public List<String> getTagsNameByBlogId(Integer bid) {
        return tagMapper.getTagsNameByBlogId(bid);
    }

    @Override
    @Transactional
    public boolean changeBlogTag(Integer bid, List<String> tags) {

        //保存新标签
        for (String tag : tags) {
            try {
                tagMapper.addStringTag(tag, System.currentTimeMillis());
            } catch (Exception ignored) {}
        }

        //旧的标签
        List<String> tagsNameByBlogId = tagMapper.getTagsNameByBlogId(bid);

        /**
         * 1 2 3
         * 2 3 4
         * 删除1，添加4
         */
        //删除旧标签中不存在于新标签中的标签
        HashSet<String> shouldDelete = new HashSet<>(tagsNameByBlogId);
        for (String tag : tags) {
            shouldDelete.remove(tag);
        }
        for (String s : shouldDelete) {
            tagMapper.removeBlogTag(bid, s);
        }

        //添加新标签中不存在于旧标签中的标签
        HashSet<String> shouldAdd = new HashSet<>(tags);
        for (String tag : tagsNameByBlogId) {
            shouldAdd.remove(tag);
        }
        for (String tag : shouldAdd) {
            int tagId = tagMapper.getTagIdByTagName(tag);
            tagMapper.applyTag(bid, tagId);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean changeArticleTag(Integer articleId, List<String> tags) {
        //保存新标签
        for (String tag : tags) {
            try {
                tagMapper.addStringTag(tag, System.currentTimeMillis());
            } catch (Exception ignored) {}
        }

        //旧的标签
        List<String> tagsNameByBlogId = tagMapper.getTagsNameByBlogId(articleId);

        /**
         * 1 2 3
         * 2 3 4
         * 删除1，添加4
         */
        //删除旧标签中不存在于新标签中的标签
        HashSet<String> shouldDelete = new HashSet<>(tagsNameByBlogId);
        for (String tag : tags) {
            shouldDelete.remove(tag);
        }
        for (String s : shouldDelete) {
            tagMapper.removeBlogTag(articleId, s);
        }

        //添加新标签中不存在于旧标签中的标签
        HashSet<String> shouldAdd = new HashSet<>(tags);
        for (String tag : tagsNameByBlogId) {
            shouldAdd.remove(tag);
        }
        for (String tag : shouldAdd) {
            int tagId = tagMapper.getTagIdByTagName(tag);
            tagMapper.applyTag(articleId, tagId);
        }

        return true;
    }
}
